﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Drawing;
using BCDataCenter.DataAccessLayer;



public partial class WebUserControl_ControlBaoCaoTVVDangKyTrongKy : System.Web.UI.UserControl
{

    static DataTable db;
    string error = "";
    ClassExecute.ClassExecute dt;
    string str = BCDataCenter.Configuration.CurrentConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            getListTimeReport();
           
            BindToDrop();
            if (Session["UserType"].ToString() == "ad")
            {
                 
                DropTTPP.SelectedValue = Session["sitecode"].ToString();
                DropTTPP.Enabled = true;
            }
            else
            {
                DropTTPP.SelectedValue = Session["sitecode"].ToString();
                DropTTPP.Enabled = false;
                
            }
        }
        else
        {
            try
            {
                Title();
                this.ASPxPivotGrid1.DataSource = db;
                this.ASPxPivotGrid1.DataBind();
            }
            catch
            {
                return;
            }
        }
    }
    private void getListTimeReport()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable dbMonth = dt.Get_Data_DataTable(str, ref error, "k_getMonthList", CommandType.StoredProcedure);

        DropDownListDateReport.DataSource = dbMonth;
            DropDownListDateReport.DataTextField = "Names";
            DropDownListDateReport.DataValueField = "Values";
            DropDownListDateReport.DataBind();
       
    }
    public void Title()
    {
        if (!ASPxPivotGrid1.Fields.Contains(ASPxPivotGrid1.Fields["Baocao"]))
        {
            ASPxPivotGrid1.Fields.Add("Baocao", DevExpress.XtraPivotGrid.PivotArea.FilterArea);
            ASPxPivotGrid1.Fields["Baocao"].HeaderStyle.BackColor = Color.Yellow;
            ASPxPivotGrid1.Fields["Baocao"].Caption = "Báo cáo TVV mới đăng ký trong kỳ";
        }

        if (!ASPxPivotGrid1.Fields.Contains(ASPxPivotGrid1.Fields["Time"]))
        {
            ASPxPivotGrid1.Fields.Add("Time", DevExpress.XtraPivotGrid.PivotArea.FilterArea);
            ASPxPivotGrid1.Fields["Time"].HeaderStyle.BackColor = Color.Yellow;
            ASPxPivotGrid1.Fields["Time"].Caption = "Ngày giờ lấy báo cáo : " + DateTime.Now.ToShortDateString() + "  " + DateTime.Now.ToShortTimeString();
        }
    }
    void BindToDrop()
    {
         
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "GET_SITENAME_ALL", CommandType.StoredProcedure);
        DropTTPP.DataSource = db;
        DropTTPP.DataTextField = "sitename";
        DropTTPP.DataValueField = "Sitecode";
        DropTTPP.DataBind();
    }
    private void get_data()
    {
        
            try
            {
                dt = new ClassExecute.ClassExecute();
                db = dt.Get_Data_DataTable(str, ref error, "[ls_getAllRecBAInTerms_CS2011]", CommandType.StoredProcedure,
                    new SqlParameter("@UserName", Session["username"].ToString()),
                    new SqlParameter("@YyyyMm", DropDownListDateReport.SelectedValue.ToString().Substring(2) + DropDownListDateReport.SelectedValue.ToString().Substring(0, 2)),
                    new SqlParameter("@mSiteCode",DropTTPP.SelectedValue.ToString()));

                ASPxPivotGrid1.Fields.Clear();
                Title();
                ASPxPivotGrid1.CustomizationFieldsLeft = 500;
                ASPxPivotGrid1.CustomizationFieldsTop = 500;
                ASPxPivotGrid1.OptionsDataField.AreaIndex = 0;

                foreach (DataColumn col in db.Columns)
                {
                    bool check = IsNumeric(col.ToString().Substring(0, 1));

                    if (check)
                    {
                        ASPxPivotGrid1.Fields.Add(col.ColumnName.ToString(), DevExpress.XtraPivotGrid.PivotArea.DataArea);
                    }
                    else
                    {
                        ASPxPivotGrid1.Fields.Add(col.ColumnName.ToString(), DevExpress.XtraPivotGrid.PivotArea.RowArea);
                    }

                }

                for (int j = 15; j < ASPxPivotGrid1.Fields.Count; j++)
                {
                    ASPxPivotGrid1.Fields[j].ValueFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                    ASPxPivotGrid1.Fields[j].ValueFormat.FormatString = "{0:###,###}";

                    ASPxPivotGrid1.Fields[j].CellFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                    ASPxPivotGrid1.Fields[j].CellFormat.FormatString = "{0:###,###}";
                }
                ASPxPivotGrid1.DataSource = db;
                ASPxPivotGrid1.DataBind();
            }
            catch (Exception ex)
            {
                uscMsgBox1.AddMessage(ex.Message, uscMsgBox.enmMessageType.Error);
            }
       
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            get_data();
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {
            using (MemoryStream filestream = new MemoryStream())
            {

                string contentType = "application/ms-excel";
                string fileName = System.DateTime.Now.ToString("dd/MM/yyyy") + "_BaoCaoTVVDangKyTrongKy.xls";
                ASPxPivotGridExporter1.ExportToXls(filestream);
                byte[] buffer = filestream.GetBuffer();
                Response.Clear();
                Response.Buffer = false;
                Response.AppendHeader("Content-Type", contentType);
                Response.AppendHeader("Content-Transfer-Encoding", "binary");
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
                Response.BinaryWrite(buffer);
                Response.End();
            }
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }

    public bool IsNumeric(string anyString)
    {
        if (anyString == null)
        {
            anyString = "";
        }
        if (anyString.Length > 0)
        {
            double dummyOut = new double();
            System.Globalization.CultureInfo cultureInfo =
                new System.Globalization.CultureInfo("en-US", true);

            return Double.TryParse(anyString, System.Globalization.NumberStyles.Any,
                cultureInfo.NumberFormat, out dummyOut);
        }
        else
        {
            return false;
        }
    }
}
